package miao.demo.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAdvice {
    @Pointcut("execution(* miao.demo.dao.BookDao.*(..))")
    public void pt(){}

    //@Before("pt()")
    public void before(){
        System.out.println("before advice ...");
    }

    //@After("pt()")
    public void after(){
        System.out.println("after advice ...");
    }

    /*
        (1)若原始方法的调用可以不接受返回值，则通知方法可以设置成void，反之则必须设定为Object类型
        (2)环绕通知的返回可以覆盖原始切入点方法执行的结果,但是返回值类型必须以和原始切入点返回类型一致
     */
    //@Around("pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("around before advice ...");
        //表示对原切入点方法操作的执行
        Object proceed = pjp.proceed();
        System.out.println("around after advice ...");
        return Integer.valueOf(proceed + "55");
    }

    //只有方法正常运行结束才能触发
    @AfterReturning("pt()")
    public void afterReturning(){
        System.out.println("afterReturning advice ...");
    }

    //程序有异常才会触发
    @AfterThrowing("pt()")
    public void afterThrow(){
        System.out.println("afterThrow advice ...");
    }
}
